package cn.example.common.protocol.modules;

import org.artifact.core.lang.IServer;
import org.artifact.core.context.session.ISession;
import org.artifact.core.context.packet.IPacket;
import cn.hutool.core.map.MapProxy;
import java.util.function.Consumer;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import org.artifact.core.constant.ArtifactErrorCode;
import org.artifact.core.exception.ArtifactExceptionUtil;
import org.artifact.core.lang.IModule;
import cn.example.common.protocol.structs.*;

@SuppressWarnings("unchecked")
public interface ChatModule extends IModule{
	
	default int getModuleId(){
		return 4952;
	}
	
	/** 分发  */
	default Consumer<IPacket> disp(ISession session,IPacket packet) {
		switch (packet.getMethodId()) {
			case 16223:{
				return sendMessage(session,packet);
			}
			default:
				throw ArtifactExceptionUtil.error(ArtifactErrorCode.NOT_FOND_ACTION);
		}
	}	


	/** 发送聊天 */
	default Consumer<IPacket> sendMessage(ISession session,IPacket packet) {
		MapProxy proxy = MapProxy.create(packet.getBody());
		int chatType = proxy.getInt(1437412018);
		java.lang.String content = proxy.getStr(951530617);
		return sendMessage(session, chatType, content);
	}
	
	/** 发送聊天 */
	Consumer<IPacket> sendMessage(ISession session, int chatType, java.lang.String content);
	
	default Consumer<IPacket> _sendMessage(ISession session){
		return (IPacket packet) -> {
                if (packet == null){
                    packet = IServer.me().getContext().getPacketFactory().create();
                    packet.setCmd(4952 << 16 ^ 16223);
                } else {
                    packet.getBody().clear();
                }

				session.sendPacket(packet);
			};
	}
	
	
	/** 推送聊天 */
	default void notifyMessage(ISession session, ChatMsgDTO msg){
		IPacket packet = IServer.me().getContext().getPacketFactory().create();
		packet.setCmd(4952 << 16 ^ 27774);
		packet.getBody().put(108417,msg);
		session.sendPacket(packet);
	}
	
	default void notifyMessage(Collection<ISession> sessions, ChatMsgDTO msg){
		IPacket packet = IServer.me().getContext().getPacketFactory().create();
		packet.setCmd(4952 << 16 ^ 27774);
		packet.getBody().put(108417,msg);
		for (ISession session : sessions) {
			session.sendPacket(packet);
		}
	}
	
}